package com.jabo.douban.demo.presentation.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jabo.douban.demo.R;
import com.jabo.douban.demo.databinding.RecommendedFragmentBinding;
import com.jabo.douban.demo.model.entity.NewsBean;
import com.jabo.douban.demo.presentation.view.adapter.RecommendRecyclerViewAdapter;
import com.jabo.douban.demo.presentation.viewmodel.RecommendedViewModel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

public class RecommendFragment extends Fragment {

    private RecommendedViewModel mViewModel;
    private RecommendedFragmentBinding mFragmentBinding;
    private RecommendRecyclerViewAdapter mViewAdapter;
    private boolean mIsFreshLoad;

    public static RecommendFragment newInstance() {
        return new RecommendFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mFragmentBinding = DataBindingUtil.inflate(inflater, R.layout.recommended_fragment, container, false);
        return mFragmentBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(RecommendedViewModel.class);
        mViewModel.getLiveData().observe(requireActivity(), new Observer<NewsBean>() {
            @Override
            public void onChanged(NewsBean newsBean) {
                if (mIsFreshLoad) {
                    mViewAdapter.setDatas(newsBean.getNewslist());
                    mFragmentBinding.smartRefresh.finishRefresh(800);
                } else {
                    mViewAdapter.addDatas(newsBean.getNewslist());
                    mFragmentBinding.smartRefresh.finishLoadMore(800);
                }
            }
        });

        mViewAdapter = new RecommendRecyclerViewAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mFragmentBinding.recyclerView.setLayoutManager(layoutManager);
        mFragmentBinding.recyclerView.setAdapter(mViewAdapter);
//        loadData();

        mFragmentBinding.smartRefresh.setEnableLoadMore(true);
        mFragmentBinding.smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.pullLoadMore();
                mIsFreshLoad = false;
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.refreshLoad();
                mIsFreshLoad = true;
            }
        });
    }

    private void loadData() {
        //开始加载数据，自动刷新
        mFragmentBinding.smartRefresh.autoRefresh(100);
    }

}
